package com.quasar.hpatient.module.home_event;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.MessageDialog;
import com.quasar.hpatient.dialog.RadioDialog;
import com.quasar.hpatient.dialog.WarningDialog;

import java.util.Arrays;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.util.SpannableUtil;
import lib.quasar.widget.button.SlideButton;
import lib.quasar.widget.group.RadioGroup;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 首页 ==> 事件
 * created by kalu on 2018/4/25 22:23
 */
public class EventActivity extends BaseActivity<EventPresenter> implements EventView {

    public final static String TIME = "TIME";
    public final static String LIST = "LIST";
    public final static int RESULT_CODE = 111111;

    @Override
    public void onBackPressed() {

        final Intent intent = new Intent();
        intent.putParcelableArrayListExtra(LIST, getPresenter().getList());
        setResult(RESULT_CODE, intent);
        super.onBackPressed();
    }

    @Override
    public int initView() {
        return R.layout.activity_home_event;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = findViewById(R.id.activity_home_event_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else {
                final RecyclerView recycler = findViewById(R.id.activity_home_event_list);
                final LinearLayout menu1 = findViewById(R.id.home_event_menu1);
                final LinearLayout menu2 = findViewById(R.id.home_event_menu2);
                final EditText menu3 = findViewById(R.id.home_event_menu3);
                final RadioGroup group = findViewById(R.id.home_event_group);
                final EditText custom = findViewById(R.id.home_event_input);
                final String dateStr = getIntent().getStringExtra(TIME);
                getPresenter().saveEvent(EventActivity.this, recycler, menu1, menu2, menu3, group, custom, dateStr);
            }
        });

        final TextView symptom = findViewById(R.id.home_event_uncomfortable_symptom);
        final TextView length = findViewById(R.id.home_event_durning_length);
        final TextView descriptions = findViewById(R.id.home_event_symptom_descriptions);
        getPresenter().initMenu(this, symptom, length, descriptions);

        final RecyclerView recycler = findViewById(R.id.activity_home_event_list);
        getPresenter().initList(this, recycler);
    }

    @Override
    public void initDataNet() {

        final RecyclerView recycler = findViewById(R.id.activity_home_event_list);
        final LinearLayout menu1 = findViewById(R.id.home_event_menu1);
        final LinearLayout menu2 = findViewById(R.id.home_event_menu2);
        final EditText menu3 = findViewById(R.id.home_event_menu3);
        final String dateStr = getIntent().getStringExtra(TIME);
        getPresenter().eventList(this, recycler, menu1, menu2, menu3, dateStr);
    }

    @Override
    public void showTime(int position) {

        final RadioDialog dialog = new RadioDialog(this);
        dialog.setOnMethodChangeListener(method -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_event_list);
            final LinearLayout menu1 = findViewById(R.id.home_event_menu1);
            final LinearLayout menu2 = findViewById(R.id.home_event_menu2);
            final EditText menu3 = findViewById(R.id.home_event_menu3);
            final String dateStr = getIntent().getStringExtra(TIME);
            getPresenter().editEvent(EventActivity.this, recycler, menu1, menu2, menu3, method, "", "", position, dateStr);
        });
        dialog.show();
        dialog.setList(Arrays.asList("1天", "2天", "3天", "4天", "5天", "5天以上"), 2);
        dialog.setLoop(false);
        dialog.setTitle("选择持续时间");
    }

    @Override
    public void showDetail(int position, String description) {

        final MessageDialog dialog = new MessageDialog(this);
        dialog.setOnDialogChangeListener(message -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_event_list);
            final LinearLayout menu1 = findViewById(R.id.home_event_menu1);
            final LinearLayout menu2 = findViewById(R.id.home_event_menu2);
            final EditText menu3 = findViewById(R.id.home_event_menu3);
            final String dateStr = getIntent().getStringExtra(TIME);
            getPresenter().editEvent(EventActivity.this, recycler, menu1, menu2, menu3, "", "", message, position, dateStr);
        });
        dialog.show();
        dialog.setInput(description);
        dialog.setTitle("症状描述");
    }

    @Override
    public void showDelete(int position, String message) {

        final WarningDialog dialog = new WarningDialog(this);
        dialog.show();
        SpannableUtil.append("确定删除", getResources().getColor(R.color.color_black));
        SpannableUtil.append(message, getResources().getColor(R.color.color_bg_theme));
        SpannableUtil.append("这条事件吗?", getResources().getColor(R.color.color_black));
        dialog.setTitles(SpannableUtil.build());
        dialog.setButton("取消", "确定");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (left) return;
            final RecyclerView recycler = findViewById(R.id.activity_home_event_list);
            final LinearLayout menu1 = findViewById(R.id.home_event_menu1);
            final LinearLayout menu2 = findViewById(R.id.home_event_menu2);
            final EditText menu3 = findViewById(R.id.home_event_menu3);
            final String dateStr = getIntent().getStringExtra(TIME);
            getPresenter().deleteEvent(EventActivity.this, recycler, position, menu1, menu2, menu3, dateStr);
        });
    }

    @Override
    public boolean isSpecital() {
        final SlideButton button = findViewById(R.id.patient_event_specital);
        return button.isChecked();
    }
}